import pandas as pd
import datetime

def calc_factor_hold_percent():
    pass

def loop_monthly():
    pass


def read_multi_price_data():
    df = pd.read_csv('e:\\stock_price_hs300_ansi_v2.csv', encoding='gb2312')

    # 第一行是 “开盘价	最高价	最低价	收盘价	成交量	成交额”， 不是真正的数据， 删掉
    df.drop(labels=0, axis=0, inplace=True)

    # 左上角那格如果在excel里是空的，读取后一般被命名为 "unnamed: 0"
    df.rename(columns={"Unnamed: 0": "日期"}, inplace=True)

    step = 6 # 以6列为宽度循环 open high low close

    columns = list(df.columns)  # 把第一列变成 list，因为pandas column 的自动去重功能，所以会在  0001HK后边自动加上编号 .1 .2 .3
    a = columns[1:7]
    count_cols = len(df.columns)
    count_stock = int((count_cols - 1) / step)

    list_df = []
    for i in range(count_stock):
        # 通过循环，把大df中每个股票的数据（6列）剥离出来
        start_column_num = i * step + 1 # 起始列是 1, 7,  13 , 19 ...
        end_column_num = start_column_num + step
        selected_columns = columns[start_column_num: end_column_num]
        symbol = selected_columns[0]

        selected_columns = ["日期"] + selected_columns  # 第0列是日期，每次都要加上
        print(i, selected_columns)
        df_one_stock = df[selected_columns].copy()  # 用表头作为筛选依据, 把每6列（算上日期7列），单独拿出来构建一个单只股票df
        # print(df_one_stock)

        # 重构表头
        df_one_stock.columns = ["日期", "开盘价", "最高价", "最低价", "收盘价", "成交量", "成交额"]

        # 添加一列数据
        df_one_stock["symbol"] = symbol

        print(df_one_stock)

        # 将剥离的“一只股票的df”，放入一个大list中
        list_df.append(df_one_stock)
        #
        # if i > 20:
        #     break

    # 将list进行前后拼接
    df_final = pd.concat(list_df)
    print(df_final)

    return df_final


if __name__ == '__main__':

    read_multi_price_data()

    # 读取数据
    df_stock_connect = pd.read_csv('e:\\stock_china_hk_connect_holding.csv')
    df_stock_connect["date"] = pd.to_datetime(df_stock_connect["date"], format="%Y-%m-%d")
    print(df_stock_connect.dtypes)

    df_price = pd.read_csv("e:\\stock_price_hs300.csv")
    df_price["date"] = pd.to_datetime(df_price["date"], format="%Y-%m-%d")
    print(df_price.dtypes)

    df_bemchmark = pd.read_csv('e:\\index_price.csv')
    df_bemchmark["date"] = pd.to_datetime(df_bemchmark["date"], format="%Y-%m-%d")
    print(df_bemchmark.dtypes)

    df_hs300 = pd.read_csv("e:\\HS300_Instrument_list.csv")

    # print(df_price)
    # #
    # price_by_symbol = {}  # 建立容器
    # price_group = df_price.groupby("symbol")
    # #
    # for group in price_group:
    #     symbol = group[0]
    #     df_data = group[1]
    #     price_by_symbol[symbol] = df_data  # 向容器中填充

    begin_date = datetime.datetime(2020, 1, 1)
    end_date = datetime.datetime(2021, 2, 1)

    # 月频数据计算
    last_date = None
    for index, row in df_bemchmark.iterrows():
        cur_date = row["date"]
        if cur_date < begin_date:
            continue
        print(cur_date)
        if last_date != None and cur_date.month != last_date.month:
            # 筛选指定日期的沪港通剖面数据
            df_stock_connect_tmp = df_stock_connect[df_stock_connect["date"] == cur_date]
            df_stock_connect_tmp = df_stock_connect_tmp[["symbol", "holding_volume"]]

            # 筛选指定日期的
            df_price_tmp = df_price[df_price["date"] == cur_date]
            df_price_tmp = df_price_tmp[["symbol", "total_shares", "free_float_shares", "close"]]

            # 三表拼接
            df_factor = pd.merge(df_hs300, df_stock_connect_tmp, how="left", on="symbol")
            df_factor = pd.merge(df_factor, df_price_tmp, how="left", on="symbol")

            # 处理空数据
            df_factor.fillna(0, inplace=True)

            # 计算因子
            df_factor["hold_percent"] = df_factor["holding_volume"] / df_factor["free_float_shares"]

            # 因子输出
            print(df_factor)
            df_factor.to_csv("e:\\china_stock_connect_holding_percent_" + cur_date.strftime('%Y-%m-%d') + ".csv", encoding="utf_8_sig")
        #
        last_date = cur_date